package com.ybfenpan.service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.ybfenpan.model.*;
import com.ybfenpan.model.log.*;
import com.ybfenpan.service.core.BaseService;
import com.ybfenpan.service.log.LogEleService;
import com.ybfenpan.service.log.LogMoneyService;
import com.ybfenpan.service.log.LogShuiService;
import com.ybfenpan.service.log.LogStoreService;

import com.ybfenpan.utils.StringUtil;
import org.springframework.stereotype.Service;

@Service
public class GongService extends BaseService {

    @Resource
    private ParamWageService paramWageService;

    @Resource
    private MemberService memberService;

    @Resource
    private WageCurrentService wageCurrentService;

    @Resource
    private LogMoneyService logMoneyService;

    @Resource
    private LogShuiService logShuiService;

    @Resource
    private DailiService dailiService;


    public Gong executFirstGong() {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("where", "1=1");
        param.put("pagination.orderBy", " id asc");  //排列

        List<Gong> list = this.queryForList("select", param);

        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    /**
     * 根据登录名查询点位
     *
     * @param loginName
     * @return
     */
    public Gong executGongByLoginName(String loginName) {
        if (StringUtil.isNotBlank(loginName)) {
            Member member = this.memberService.executMemberByLoginName(loginName);
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("memberId", member.getId());
            List<Gong> gongs = queryForList("select", param);
            if (null != gongs && 0 < gongs.size()) {
                return gongs.get(0);
            }
        }
        return null;
    }

    /**
     * @功能 进入公排 type 20 自动进来 不扣消费积分 10进来扣
     */
    public void saveJoinGong(Member member) {

        /******************* 保存该对象公排信息 *******************/
        // 找公排的接点人

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("where", "(gongZuoId is null or gongYouId is null) and gongCeng is not null   order by gongDate asc ");
        List<Gong> list = this.queryForList("select", param);

        Gong gongFather = null; // 公排接点人


	/*	Map<String, Object> mapEle = new HashMap<String, Object>();
        mapEle.put("id", member.getId());
		mapEle.put("diyUpdate", "gongNum=gongNum+1");
		this.memberService.update(mapEle);*/

        Gong gongMember = new Gong(); // 新的公排记录
        gongMember.setMemberId(member.getId());
        gongMember.setNumber(0d);
//		gongMember.setQuyu(member.getQuyu());
        this.saveOrUpdate(gongMember);

        // 获得公排接点人
        if (list.size() > 0) {
            gongFather = list.get(0);
        }

        if (gongFather != null) {
            // 设置自己和公排接点人的关系;
            if (gongFather.getGongZuoId() == null) {
                gongFather.setGongZuoId(gongMember.getId());
                gongMember.setGongQu(1);
            } else if (gongFather.getGongYouId() == null) {
                gongFather.setGongYouId(gongMember.getId());
                gongMember.setGongQu(2);
            }

            gongMember.setGongFatherId(gongFather.getId()); // 公排接点人
            gongMember.setGongCeng(gongFather.getGongCeng() + 1); // 公排所属层数

            this.update(gongFather);

        } else {
            gongMember.setGongQu(1);
            gongMember.setGongCeng(1); // 公排所属层数
        }

        gongMember.setGongZuoNum(0); // 公排左区业绩
        gongMember.setGongYouNum(0); // 公排右区业绩

        gongMember.setGongZuoRen(0); // 公排左区人数
        gongMember.setGongYouRen(0); // 公排右区人数

        gongMember.setGongZuoCeng(0); // 公排左区层数
        gongMember.setGongYouCeng(0); // 公排右区层数

        gongMember.setGongDate(new Date()); // 进入公排时间

        this.saveOrUpdate(gongMember);

        /**************** 计算公排方面的工资 ********************/
        Gong child = gongMember;
        Integer fatherId = gongMember.getGongFatherId(); // 取接点人Id

        Gong father = this.getByKey(fatherId); // 取接点人

        Integer ceng = 1; // 计算到的层数

        while (father != null) {

            /******************* 修改数据 *************************/

            // 如果儿子为左区，则增加父亲左区单数和业绩
            if (child.getGongQu().equals(1)) {
                father.setGongZuoRen(father.getGongZuoRen() + 1); // 左区单数
                father.setGongZuoNum(father.getGongZuoNum()+member.getPayNum().intValue());
            }

            // 如果儿子为右区，则增加父亲右区单数和业绩
            if (child.getGongQu().equals(2)) {
                father.setGongYouRen(father.getGongYouRen() + 1); // 右区单数
                father.setGongYouNum(father.getGongYouNum()+member.getPayNum().intValue());
            }

            this.update(father);

            /*************** 计算公排见点奖 *****************/
            Integer memberId = father.getMemberId();
            System.out.println("-----memberId------" + memberId);


            Member fatherMember = this.memberService.getByKey(memberId);
            Daili daili = this.dailiService.getByKey(fatherMember.getDailiId());


            Double wage2 = daili.getJianDian();


            if (fatherMember.getRecommendYe() >= daili.getYeJi()) { // 达到
                if (daili.getJianDianCeng()>=ceng ) {
                    this.countWage2(fatherMember, wage2, member, gongMember.getId());
                }
            } else {//都没有达到要求赠送相应的层数
                if (daili.getUnJianDianCeng()>=ceng ) {
                    this.countWage2(fatherMember, wage2, member, gongMember.getId());
                }
            }

            // 取下一个father
            child = father;
            Integer fId = father.getGongFatherId();
            father = this.getByKey(fId);
            ceng++; // 加一层

        }

    }

    /**
     * @功能 计算 见点奖
     */
    public void countWage2(Member member, Double wage2, Member one, Integer gongId) {

        if (wage2 > 0) {

            String title = "";
            // 判断该代理商是否停薪
            if (member.getStateWage().equals("F")) {
                wage2 = 0d;
                title = title + "。(工资已冻结)";
            }

            if (member.getAccountUse().equals("F")) {
                wage2 = 0d;
                title = title + "。(账户已冻结)";
            }


            Map<String, Object> memberMap = new HashMap<String, Object>();
            memberMap.put("id", member.getId());
            memberMap.put("diyUpdate", "money = money +" + wage2 + ",wage2=wage2+" + wage2 + ",historyWage=historyWage+" + wage2 );
            this.memberService.update(memberMap);
            member = this.memberService.getByKey(member.getId());
            Double yuMoney = member.getMoney();
            LogMoney log2 = new LogMoney();
            log2.setMemberId(member.getId()); // 所属代理商
            log2.setMemberLoginName(member.getLoginName());
            title = "幸运奖，代理商姓名：" + one.getLoginName() + title;
            // 日志说明

            log2.setTitle(title);
            log2.setState(2); // 类型
            log2.setNum(wage2); // 数量
            log2.setTotalNum(wage2);
            log2.setIsFa("T");
            log2.setDongNum(0d);
            log2.setYu(yuMoney);
            log2.setCommitDate(new Date());
            log2.setFaDate(new Date()); // 发放时间
            this.logMoneyService.saveOrUpdate(log2);

            // 改动当前工资
            Integer wageCurrentId = member.getWageCurrentId();
            Map<String, Object> paramWageCurrent = new HashMap<String, Object>();
            paramWageCurrent.put("id", wageCurrentId);
            paramWageCurrent.put("diyUpdate", " wage2=wage2+" + wage2 + ",wageNum=WageNum+" + wage2);
            this.wageCurrentService.update(paramWageCurrent);

        }
    }

}
